﻿#include "BrowserTableHandlerMaterialFE.h"
#include "OperatorsInterface/OperAbstractMaterial.h"

#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialElastic.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDensity.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialExpansion.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDamping.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialBehavior.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"

#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"

void GUI::BrowserTableHandlerMaterialFE::updateTable()
{
    if (_tableWidget == nullptr) return;
    _tableWidget->clear();
    _tableWidget->setColumnCount(0);
    _tableWidget->setRowCount(0);

//设置表头
    QStringList headerLabels;
    headerLabels << tr("Name") << tr("1") << tr("MID") << tr("3")
        << tr("4") << tr("5") << tr("6") << tr("7") << tr("8") << tr("9") << tr("Notes");
    _tableWidget->setColumnCount(headerLabels.size());
    _tableWidget->setHorizontalHeaderLabels(headerLabels);
//更新表格
    this->updateData();
}

void GUI::BrowserTableHandlerMaterialFE::updateData()
{
    if (_tableWidget == nullptr) return;

    Acoustics::FITKAcousticsDiscModel* disc = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
    if (!disc)return;
    Acoustics::FITKAcousticsFEModel* feModel = disc->getFEModel();
    if (!feModel) return;

//获取材料管理器
    Interface::FITKMaterialManager* matmgr = feModel->getMaterialManager();
    if (matmgr == nullptr) return;

 //材料数量
    const int n = matmgr->getDataCount();
    _tableWidget->setRowCount(n);
 //待设数据
    double E = 0;
    double NU = 0;
    double RHO = 0;
    double A = 0;
    double TREF = 0;
    double GE = 0;
    double G = 0/*E / (2 + 2 * NU)*/;

    for (int i = 0; i < n; ++i)
    {
    //转换为FE材料
        Interface::FITKAbaMaterial* FE = dynamic_cast<Interface::FITKAbaMaterial*>(matmgr->getDataByIndex(i));
        if (FE == nullptr) continue;
    //数据ID
        const int id = FE->getDataObjectID();

        Interface::FITKAbaMaterialElastic* elasticBevior = dynamic_cast<Interface::FITKAbaMaterialElastic*>
            (FE->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Elastic));
        if (elasticBevior){
            E = elasticBevior->getIsotropicYoungsModulus();
            NU = elasticBevior->getIsotropicPoisssonRatio();
        }
        Interface::FITKAbaMaterialDensity* densityBehavior = dynamic_cast<Interface::FITKAbaMaterialDensity*>
            (FE->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Density));
        if (nullptr != densityBehavior){
            RHO = densityBehavior->getMassDensity();
        }
        Interface::FITKAbaMaterialExpansion* expensionBehavior = dynamic_cast<Interface::FITKAbaMaterialExpansion*>
            (FE->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Expansion));
        if (nullptr != expensionBehavior){
            TREF = expensionBehavior->getIsotropicTemperature();
            A = expensionBehavior->getIsotropicExpansionCoeff();
        }
        Interface::FITKAbaMaterialDamping* dampingBehavior = dynamic_cast<Interface::FITKAbaMaterialDamping*>
            (FE->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Damping));
        if (nullptr != dampingBehavior){
            GE = dampingBehavior->getStructural();
        }

    //填充表格，用type表示ID
        _tableWidget->setItem(i, 0, new QTableWidgetItem(FE->getDataObjectName(), id));
        _tableWidget->setItem(i, 1, new QTableWidgetItem("MAT1", id));
        _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(id), id));
        _tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(E), id));
        _tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(E / (2 + 2 * NU)), id));
        _tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(NU), id));
        _tableWidget->setItem(i, 6, new QTableWidgetItem(QString::number(RHO), id));
        _tableWidget->setItem(i, 7, new QTableWidgetItem(QString::number(A), id));
        _tableWidget->setItem(i, 8, new QTableWidgetItem(QString::number(TREF), id));
        _tableWidget->setItem(i, 9, new QTableWidgetItem(QString::number(GE), id));
        _tableWidget->setItem(i, 10, new QTableWidgetItem(FE->getMaterialDescribe(), id));
    }
    _tableWidget->autoResizeCellWidth();
    _tableWidget->setTableItemEditable(false);
}

EventOper::OperAbstractMaterial * GUI::BrowserTableHandlerMaterialFE::getMaterialOper()
{
 //从操作器仓库获取
    return Core::FITKOperatorRepo::getInstance()->
        getOperatorT<EventOper::OperAbstractMaterial>("OperMaterialFE");
}
